#
# Author: pepone.onrez@gmail.com
# License: GPL-v3
#
# This cmake script generate rules for copy generated files to convenient places
# in the project structure.
# There are to macros on for move the headers other for move sources.
# Maybe a single macro with the directory as a input param is more convenient here
#

SET ( MOVE_FILE_COMMAND mv )

MACRO( APPEND ORIGINAL NEW_BIT )

  IF    ( NOT ${ORIGINAL} )
    SET( ${ORIGINAL} ${NEW_BIT} )
  ELSE  ( NOT ${ORIGINAL} )
    SET( ${ORIGINAL} ${${ORIGINAL}} ${NEW_BIT} )
  ENDIF ( NOT ${ORIGINAL} )

ENDMACRO( APPEND ORIGINAL NEW_BIT )


# This macro take is input arg as a file name and generate a rule for copy to src/generated
# if the file has changed. It alos generate a custo target GENERATED_SOURCES that you can DEPEND on
# for force the commands being call

MACRO ( ADD_TO_GENERATED_SOURCES GENERATED_CPP_SOURCES )

    FOREACH( CPP_SOURCE_FILE ${ARGN} )
        APPEND( ALL_INPUT_SOURCES "${CPP_SOURCE_FILE}" )
    ENDFOREACH( CPP_SOURCE_FILE ${ARGN} )

    FOREACH( CPP_SOURCE_FILE ${ARGN} )

        GET_FILENAME_COMPONENT( CPP_SOURCE_BASENAME ${CPP_SOURCE_FILE} NAME )
        APPEND( GEN_CPP_SOURCES "${PROJECT_SOURCE_DIR}/src/generated/${CPP_SOURCE_BASENAME}" )
        MESSAGE( "source file: ${PROJECT_SOURCE_DIR}/src/generated/${CPP_SOURCE_BASENAME}" )
        MESSAGE("depends on: ${CPP_SOURCE_FILE}" )

        ADD_CUSTOM_COMMAND(
            OUTPUT ${PROJECT_SOURCE_DIR}/src/generated/${CPP_SOURCE_BASENAME}
            DEPENDS ${CPP_SOURCE_FILE}
            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CPP_SOURCE_FILE} ${PROJECT_SOURCE_DIR}/src/generated/
            COMMENT " --- Copying file ${CPP_SOURCE_FILE} ")

    ENDFOREACH( CPP_SOURCE_FILE ${ARGN} )

    ADD_CUSTOM_TARGET(GENERATED_SOURCES DEPENDS ${GEN_CPP_SOURCES})

    SET ( GENERATED_CPP_SOURCES ${GEN_CPP_SOURCES} )
ENDMACRO ( ADD_TO_GENERATED_SOURCES GENERATED_CPP_SOURCES )


# This macro is esentialy equal to above but move files to include/generated.
# This tow need to merge in a single macro that can do both.
# Other need thing must be set the ADD_CUSTOM_TARGET name as a user variable.
# in this way we can use the macro several times in a CMakeScript for comping
# diferent files.
#

MACRO ( ADD_TO_GENERATED_HEADERS GENERATED_CPP_HEADERS )

    FOREACH( CPP_HEADER_FILE ${ARGN} )
        APPEND( ALL_INPUT_HEADERS "${CPP_HEADER_FILE}" )
    ENDFOREACH( CPP_HEADER_FILE ${ARGN} )


    FOREACH( CPP_HEADER_FILE ${ARGN} )
        GET_FILENAME_COMPONENT(CPP_HEADER_BASENAME ${CPP_HEADER_FILE} NAME)
        APPEND( GEN_CPP_HEADERS "${PROJECT_SOURCE_DIR}/include/generated/${CPP_HEADER_BASENAME}" )

        MESSAGE( "header file: ${PROJECT_SOURCE_DIR}/include/generated/${CPP_HEADER_BASENAME}" )
        MESSAGE("depends on: ${CPP_HEADER_FILE}" )

        ADD_CUSTOM_COMMAND(
            OUTPUT ${PROJECT_SOURCE_DIR}/include/generated/${CPP_HEADER_BASENAME}
            DEPENDS ${CPP_HEADER_FILE}
            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CPP_HEADER_FILE} ${PROJECT_SOURCE_DIR}/include/generated/
            COMMENT " --- Copying file ${CPP_HEADER_BASENAME} ")

    ENDFOREACH( CPP_HEADER_FILE ${ARGN} )

    ADD_CUSTOM_TARGET(GENERATED_HEADERS DEPENDS ${GEN_CPP_HEADERS})

    SET( GENERATED_CPP_HEADERS ${GEN_CPP_HEADERS} )
ENDMACRO ( ADD_TO_GENERATED_HEADERS GENERATED_CPP_HEADERS )

